package handlers

import (
	"bloggo/bloggo"
	"bloggo/dto"
	"bloggo/service"
	"bloggo/structs"
	"context"
	"encoding/json"
	"gitee.com/chunanyong/zorm"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"strconv"
	"time"
)

// Messages 留言列表
func Messages(c *gin.Context) *bloggo.R {
	page := zorm.NewPage()
	page.PageSize = 100
	finder := zorm.NewFinder()
	finder.Append("SELECT * FROM").Append(structs.TbMessageStructTableName).Append("ORDER BY create_time ASC")
	var data []structs.TbMessageStruct
	err := zorm.Query(c, finder, &data, page)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	return bloggo.OkData(data)
}

// PostMessages 我要留言
func PostMessages(c *gin.Context) *bloggo.R {
	dataBytes, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	message := structs.TbMessageStruct{}
	err = json.Unmarshal(dataBytes, &message)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	//TODO 2022/11/17 留言是否审核
	message.IsReview = 1
	message.CreateTime = time.Now()
	ipAddress, err := bloggo.GetIpAddress(c.Request)
	if err != nil {
		return bloggo.Fail()
	}
	message.IpAddress = ipAddress
	source, err := bloggo.GetIpSource(ipAddress)
	if err == nil {
		message.IpSource = source.Province
	}
	err = service.SaveTbMessageStruct(c, &message)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	return bloggo.Ok()
}

// AdminMessages 留言管理列表
func AdminMessages(c *gin.Context) *bloggo.R {
	page := zorm.NewPage()
	page.PageNo, _ = strconv.Atoi(c.DefaultQuery("current", "1"))
	page.PageSize, _ = strconv.Atoi(c.DefaultQuery("size", "10"))
	keywords := c.DefaultQuery("keywords", "")
	isReview := c.DefaultQuery("isReview", "")

	finder := zorm.NewFinder()
	finder.Append("SELECT * FROM").Append(structs.TbMessageStructTableName).Append("WHERE 1=1")
	if len(isReview) > 0 {
		finder.Append("AND is_review=?", isReview)
	}
	if len(keywords) > 0 {
		finder.Append("AND nickname LIKE ?", "%"+keywords+"%")
	}
	finder.Append("ORDER BY create_time ASC")
	var data []structs.TbMessageStruct
	err := zorm.Query(c, finder, &data, page)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	return bloggo.OkData(gin.H{
		"recordList": data,
		"count":      page.TotalCount,
	})
}

// ReviewMessages 留言消息审核
func ReviewMessages(c *gin.Context) *bloggo.R {
	var reqData dto.ReviewMessageDTO
	err := c.ShouldBind(&reqData)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	if len(reqData.IdList) <= 0 {
		return bloggo.Ok()
	}
	finder := zorm.NewFinder().Append("UPDATE").Append(structs.TbMessageStructTableName)
	finder.Append("SET is_review=?,update_time=?", reqData.IsReview, time.Now()).Append("WHERE id IN (?)", reqData.IdList)
	_, err = zorm.Transaction(c, func(c context.Context) (interface{}, error) {
		return zorm.UpdateFinder(c, finder)
	})
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	return bloggo.Ok()
}

// DeleteMessages 删除留言消息
func DeleteMessages(c *gin.Context) *bloggo.R {
	var idList []int
	err := c.ShouldBind(&idList)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	if len(idList) <= 0 {
		return bloggo.Ok()
	}
	finder := zorm.NewDeleteFinder(structs.TbMessageStructTableName)
	finder.Append("WHERE id IN (?)", idList)
	_, err = zorm.Transaction(c, func(c context.Context) (interface{}, error) {
		return zorm.UpdateFinder(c, finder)
	})
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	return bloggo.Ok()
}
